package com.handydata.antifraud.ws.model;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.csvreader.CsvReader;
import com.handydata.antifraud.annotation.Comment;
import com.handydata.antifraud.common.constant.DictInfoCode;
import com.handydata.antifraud.common.exception.WsInvokeException;
import com.handydata.antifraud.common.task.LoadPmmlRun;
import com.handydata.antifraud.service.DictInfoService;
import com.handydata.antifraud.to.dap.ModelRunReqTO;
import com.handydata.antifraud.to.dap.ModelRunResTO;
import com.handydata.antifraud.ws.AbstractWsService;
import com.handydata.antifraud.ws.WsInvocationService;
import com.handydata.common.util.RedisUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 统计上行下发的数据，返回给规则引擎用
 */
@Service("modelRunService")
@Comment("反欺诈模型")
public class ModelRunImpl extends AbstractWsService<ModelRunReqTO, ModelRunResTO>
	implements WsInvocationService<ModelRunReqTO, ModelRunResTO> {

	private static final Logger logger = LoggerFactory.getLogger(ModelRunImpl.class);

	@Autowired
	private RedisUtil redisUtil;
	@Autowired
	DictInfoService dictInfoService;

	private final  static  String paramsStr = "CateLv2_借贷_DownloadLe0.001Mean_RTO,CateLv2_银行_Score_Ge5_RTO,CateLv2_银行_Score_Ge7_Num," +
											"CateLv2_借贷_sizeLeQuan0.01_Num,CateLv2_银行_Score_Ge7_RTO,CateLv2_借贷_DownloadLe1000K_Score_Mean," +
											"CateLv2_理财工具_SizeLe0.1Mean_RTO,CateLv2_银行_SizeLe0.5Mean_RTO,CateLv2_记账_DownloadLe0.001Mean_Num," +
											"CateLv2_银行_Num,CateLv2_借贷_sizeLeQuan0.1_Num,CateLv2_借贷_sizeGeQuan0.9_Num,CateLv2_理财工具_SizeLe0.1Mean_Num," +
											"CateLv2_银行_DownloadLeQuan0.8_Score_Mean,CateLv2_借贷_SizeLe0.5Mean_RTO,CateLv2_理财工具_DownloadLeQuan0.1_Score_Mean,Score_Le4_Num,CateLv2_理财工具_sizeGeQuan0.8_Num," +
											"CateLv2_火车票_sizeLeQuan0.1_Num,CateLv1_购物_InTop10_Num,CateLv2_支付_Num,CateLv2_借贷_DownloadLe100K_Score_Mean,Download_Ge10000K_Num,CateLv2_理财工具_Score_Ge5_RTO,CateLv1_运动健康_sizeGeQuan0.8_Num,CateLv2_记账_Num," +
											"CateLv1_理财_sizeLeQuan0.01_Num,CateLv2_银行_DownloadLe0.1Mean_RTO,CateLv2_理财工具_DownloadLe100K_Score_Mean,CateLv1_理财_downloadGeQuan0.8_Num,CateLv2_火车票_DownloadLeQuan0.8_Score_Mean,CateLv1_购物_Num," +
											"CateLv2_记账_RTO,CateLv2_支付_DownloadLe0.1Mean_Num,CateLv2_商城_Score_Mean,CateLv2_商城_downloadGeQuan0.8_Num,CateLv2_租车_Num,CateLv2_支付_sizeGeQuan0.8_Num,CateLv1_理财_DownloadLeQuan0.8_Score_Mean,CateLv1_理财_Num," +
											"CateLv2_理财工具_Score_Le4_RTO,CateLv2_记账_DownloadLe0.1Mean_RTO,CateLv2_借贷_sizeGeQuan0.8_Num,CateLv2_银行_Score_Le4_Num,CateLv2_借贷_DownloadLe0.1Mean_RTO,CateLv2_股票投资_sizeGeQuan0.8_Num,CateLv2_理财工具_Score_Le3_RTO," +
										"CateLv2_商城_sizeGeQuan0.8_Num,CateLv2_借贷_downloadLeQuan0.1_Num,CateLv2_团购优惠券_Score_Mean";

	private final static  String mapStr="{\"手机京东\":\"京东\",\"优酷\":\"优酷视频\",\"手机百度\":\"百度\",\"蚂蜂窝旅游\":\"马蜂窝旅游\",\"蚂蜂窝自由行\":\"马蜂窝旅游\",\"天猫\":\"手机天猫\",\"斗鱼\":\"斗鱼直播\",\"滴滴优步司机\":\"滴滴车主\",\"高德导航\":\"高德地图\",\"卡牛\":\"卡牛信用管家\",\"给你花\":\"给你花极速版\",\"极速钱包\":\"极速口袋钱包\",\"王者荣耀助手\":\"王者荣耀皮肤助手\",\"猫眼电影\":\"猫眼\",\"极速现金侠\":\"现金侠\",\"土豆\":\"土豆视频\",\"高铁管家\":\"高铁管家-12306火车票抢票\",\"爱奇艺播放器\":\"爱奇艺\",\"银联钱包\":\"云闪付\",\"易到\":\"易到用车\",\"闪电借款领先版\":\"闪电借款\",\"车轮查违章\":\"车行易查违章\",\"前程无忧\":\"前程无忧51Job\",\"360云盘\":\"360安全云盘\",\"哈罗单车\":\"哈罗\",\"华住\":\"华住酒店\",\"随行付商务版\":\"随行付Plus\",\"快贷贷款\":\"快贷\",\"搜房网房天下\":\"房天下\",\"卡牛信用卡管家\":\"卡牛信用管家\",\"东方财富网\":\"东方财富\",\"嘀嗒拼车\":\"嘀嗒出行\",\"信用钱包-贷款易\":\"信用钱包\",\"花无缺借款\":\"花无缺\",\"企查查\":\"企查查企业信用查询\",\"万能遥控\":\"万能遥控器\",\"聚美优品\":\"聚美\",\"闪银极速版\":\"闪银-贷款借钱\",\"财付通\":\"掌声财付通\",\"手机贷-贷款借钱借款\":\"手机贷-贷款借钱借款\",\"我来贷个人版\":\"我来贷\",\"智行火车票\":\"智行火车票12306高铁抢票\",\"51闪电贷\":\"闪电贷\",\"天眼查\":\"天眼查企业查询\",\"小赢卡贷升级版\":\"小赢卡贷\",\"搜狐新闻三星版\":\"搜狐新闻\",\"苹果助手\":\"苹果查询助手\",\"功夫贷专业版\":\"功夫贷\",\"聚力体育\":\"聚力\",\"国美在线\":\"国美\",\"百度知道\":\"知道\",\"美团外卖商家\":\"美团外卖商家版\",\"格林豪泰\":\"格林\",\"金山词霸\":\"金山词霸练口语\",\"搜狐新闻曲屏版\":\"搜狐新闻\",\"微车查违章\":\"微车违章查询\",\"享骑电单车\":\"享骑出行\",\"陌陌\":\"MOMO陌陌\",\"玖富万卡新手版\":\"玖富万卡\",\"拍拍贷借款(贷款·借钱·借贷)\":\"拍拍贷借款\",\"58同城网\":\"58同城\",\"平安一账通\":\"一账通\",\"闪贷-贷款借款分期\":\"闪贷\",\"智行火车票12306购票\":\"智行火车票12306高铁抢票\",\"曹操专车\":\"曹操出行\",\"360摄像机\":\"360智能摄像机\",\"货拉拉-搬家拉货\":\"货拉拉\",\"宜人贷借款极速版\":\"宜人贷借款\",\"企鹅直播\":\"企鹅体育-NBA直播\",\"瑞银信\":\"瑞银商户\",\"百度外卖\":\"美团\",\"58速运\":\"58速运企业版\",\"虎扑体育\":\"虎扑足球\",\"聚力视频\":\"聚力短视频\",\"车轮易查违章\":\"车行易查违章\",\"蚂蚁聚宝\":\"蚂蚁财富\",\"百度钱包\":\"度小满金融\",\"映客直播\":\"映客\",\"爱彼迎\":\"Airbnb爱彼迎\",\"亲亲小贷\":\"亲亲小贷借款\",\"现金快贷\":\"现金快贷管家\",\"网易考拉海购\":\"网易考拉\",\"平安天下通\":\"平安通\",\"天天中彩票\":\"天天中\",\"部落冲突\":\"部落冲突Clash-of-Clans\",\"皇室战争\":\"部落冲突:皇室战争\",\"天龙八部\":\"天龙八部手游\",\"狂野飙车8\":\"狂野飙车\",\"猎聘同道\":\"猎聘\",\"欢乐升级\":\"QQ欢乐升级\",\"宜人贷借款（信用卡极速贷款）\":\"宜人贷借款\",\"万达财富\":\"万达贷\",\"百度外卖-美食品质外卖\":\"美团\",\"爱奇艺头条\":\"爱奇艺\",\"英雄杀\":\"英雄杀(官方版)\",\"豌豆荚\":\"豌豆荚安卓市场\",\"闪银借款\":\"闪银-贷款借钱\",\"全民夺宝\":\"全民一元夺宝\",\"北京赛车\":\"北京赛车pk10\",\"宜人贷\":\"宜人贷借款\",\"当当读书\":\"当当云阅读\",\"贷款侠\":\"贷款侠借款\",\"联璧金融\":\"联璧\",\"中石化加油卡掌上营业厅\":\"中国石化加油卡掌上营业厅\",\"百度导航\":\"百度地图\"}";

	private  static  Map nameMap;

	@Autowired
	private LoadPmmlRun loadPmmlRun;

	/**
	 * @see AbstractWsService#register()
	 */
	@PostConstruct
	@Override
	public void register() {
		registerToWsServiceFactory(this, ModelRunReqTO.class);
	}

	static {
		nameMap=JSON.parseObject(mapStr);
	}
	@Override
	public ModelRunResTO query(ModelRunReqTO req) throws WsInvokeException {

		try{

			ModelRunResTO res=new ModelRunResTO();
			if(req.getAppList()==null||req.getAppList().size()<=0)
				return res;
			if(req.getAppList().size()==1){
				Object obj=req.getAppList().get(0);
				if(obj==null||obj.toString().length()<=0){
					return  res;
				}
			}
			List distinctAppList = req.getAppList();
			List mapAppList=new ArrayList();
			List newAppList=new ArrayList();
			for(int i=0;i<distinctAppList.size();i++){
				if(nameMap.get(distinctAppList.get(i))!=null){
					newAppList.add(nameMap.get(distinctAppList.get(i)));
				}else {
					newAppList.add(distinctAppList.get(i));
				}
			}
			ArrayList arr=new ArrayList();
			newAppList = dictInfoService.getMuitleDictInfo(newAppList, DictInfoCode.DICT_APP_TYPE_MODEL.getCode());
			List<App> appList=new ArrayList();
			Map<String,List<App>> catMap=new HashMap<>();
			Map<String,List<App>> smallCatMap=new HashMap<>();
			List<App> appListM=new ArrayList();
			int sysAppCount=0;
			Map appIdDisMap=new HashMap();
			for(int i=0;i<newAppList.size();i++){
				Object obj=newAppList.get(i);
				String appName=distinctAppList.get(i)+"";
				if(obj!=null){
					String[] strs=obj.toString().split("_");
					if(strs.length==6){
						String bigcats=strs[4];
						String smallCats=strs[3];
						//String reg="[a-zA-Z .]+|^(?:意见反馈|电话|视频|邮件|音乐|照片|时钟|信息|备忘录|通话记录|相机|设置|日历|联系人|通讯录|计算器|系统|短信)$|语音备忘录|健身记录|Service|股市|帮助程序|语音服务|证书|小米|华为|vivo|SIM|System|Android|OPPO|MIUI|Google|Galaxy|服|SDK|AskPermission|Auth$|Dialog|Siri|iBooks|iMovie|iTunes|iAd|AppStore|Authenticator|CheckerBoard|打印中心|放大器|提示|用户鉴定|家庭|诊断信息|提醒事项|查找朋友|语音设置";
						String reg=".*[a-zA-Z .]+.*|^(?:意见反馈|电话|视频|邮件|音乐|照片|时钟|信息|备忘录|通话记录|相机|设置|日历|联系人|通讯录|计算器|系统|短信)$|.*语音备忘录.*|.*健身记录.*|.*Service.*|股市.*|帮助程序.*|.*语音服务.*|.*证书.*|.*小米.*|.*华为.*|.*vivo.*|.*SIM.*|.*System.*|.*Android.*|.*OPPO.*|.*MIUI.*|.*Google.*|.*Galaxy.*|.*服.*|SDK.*|.*AskPermission.*|Auth$|.*Dialog.*|Siri.*|.*iBooks.*|.*iMovie.*|.*iTunes.*|.*iAd.*|.*AppStore.*|.*Authenticator.*|.*CheckerBoard.*|.*打印中心.*|.*放大器.*|.*提示.*|.*用户鉴定.*|.*家庭.*|.*诊断信息.*|.*提醒事项.*|.*查找朋友.*|.*语音设置.*";
						if(appName.matches(reg)){//跳过系统app
							//cat="自带Aapp";
							smallCats=null;
							sysAppCount++;
							logger.info(" sys app {}",obj.toString() );
							continue;
						}
						String[] downloads=strs[0].split(",");
						String[] scores=strs[1].split(",");
						String[] sizes=strs[2].split(",");
						String[] appids=strs[5].split(",");
						String[] cats=null;
						String[] scats=null;
						if(bigcats!=null&&bigcats.length()>0) {
							cats = bigcats.split(",");
							if(cats.length!=downloads.length){
								logger.info(" app 数据有问题 {}",appName+strs);
								continue;
							}
						}
						if(smallCats!=null&&smallCats.length()>0) {
							scats = smallCats.split(",");
							if(scats.length!=downloads.length){

								logger.info(" app 数据有问题 {}",appName+strs);
								continue;
							}
						}
						if(appids!=null&&appids.length>0) {
							if(appids.length!=downloads.length){

								logger.info(" app 数据有问题 {}",appName+strs);
								continue;
							}
						}

						for(int j=0;j<downloads.length;j++){
							App app=new App();
							app.setName(appName);
							app.setDownload(getDouble(downloads[j]));
							app.setScore(getDouble(scores[j]));
							app.setSize(getDouble(sizes[j]));
							String appId=appids[j];
							app.setId(appId);
							appListM.add(app);

							boolean isDisinct=false;

							if(!appIdDisMap.containsKey(appId)){
								appIdDisMap.put(appId,1);
								isDisinct=true;
							}else{
								isDisinct=false;
							}
							if(isDisinct)
								appList.add(app);

							if(cats!=null){

								String cat=cats[j];
								String catm=cat+"_m";
								if(cat!=null||cat.length()>0){
									if(isDisinct){//只取第一个
										if(catMap.get(cat)==null)
											catMap.put(cat,new ArrayList<>());
										catMap.get(cat).add(app);
									}
								}
								if(catm!=null||catm.length()>0){
									if(catMap.get(catm)==null)
										catMap.put(catm,new ArrayList<>());
									catMap.get(catm).add(app);
								}
							}
							if(scats!=null){
								String scat=scats[j];
								String scatm=scat+"_m";

								if(scat!=null||scat.length()>0){
									if(isDisinct){//只取第一个
										if(smallCatMap.get(scat)==null)
											smallCatMap.put(scat,new ArrayList<>());
										smallCatMap.get(scat).add(app);
									}
								}
								if(scatm!=null||scatm.length()>0){
									if(smallCatMap.get(scatm)==null)
										smallCatMap.put(scatm,new ArrayList<>());
									smallCatMap.get(scatm).add(app);
								}
							}
						}

					}else{
						logger.warn("app _分割 对应数据有问题{}",appName+obj);
					}
				}else{
					logger.debug("app 没有查到字典信息{}",appName);
				}
			}
			String[] params=paramsStr.split(",");
			Map<String,Double> results=new HashMap<>();
			for(String param:params){
				if(param!=null&&(req.getParam()==null||param.equals(req.getParam()))){//&&param.equals(req.getParam())
					logger.info("开始处理参数{}",param);
					long start=System.currentTimeMillis();
					StringBuilder sb=new StringBuilder();
					sb.append(param);
					String[] strs=param.split("_");

					List<App> apps=null;
					List<App> apps2=null;//不去重的
					if(strs[0].indexOf("CateLv")==-1){//处理Score_Le4_Num这种情况
						strs=("all_all_"+param).split("_");
					}
					String catType=strs[0];
					String cat=strs[1];
					apps=smallCatMap.get(cat);
					apps2=smallCatMap.get(cat+"_m");
					if(catType.equals("CateLv1")){
						apps=catMap.get(cat);
						apps2=catMap.get(cat+"_m");
					}
					if(catType.equals("all")){
						apps=appList;
						apps2=appListM;
					}
					if(apps==null||apps.size()<=0){
						results.put(param,0D);
						continue;
					}
					String valType=null;
					String operator=null;
					String dataType=null;//quan or mean,or const
					String dataTune=null;
					String resultType=null;//Num,RTO,ScoreMean
					String action=strs[2];

					if(action.startsWith("size")||action.startsWith("Size")) valType="size";
					else if(action.startsWith("Download")||action.startsWith("download")) valType="download";
					else if(action.startsWith("Score")||action.startsWith("score")) valType="score";
					else if(action.indexOf("Top")!=-1)valType="top";
					if(action.indexOf("Le")!=-1) operator="Le";
					else if(action.indexOf("Ge")!=-1)operator="Ge";
					else if(action.indexOf("In")!=-1)operator="In";
					else  if(strs.length>3){
						action=action+strs[3];
						if(action.indexOf("Le")!=-1) operator="Le";
						else if(action.indexOf("Ge")!=-1)operator="Ge";
						else if(action.indexOf("In")!=-1)operator="In";
					}

					if(action.indexOf("Quan")!=-1||action.indexOf("Top10")!=-1){
						dataType="Quan";
					}else if(action.indexOf("Mean")!=-1){
							dataType="Mean";
					}
					dataTune=action.replace(operator+"","").replaceAll("(?i)"+valType,"").replace(dataType+"","");
					if(param.endsWith("RTO")){
						resultType="RTO";
					}else if(param.endsWith("Num")) {
						resultType="Num";
					}else {
						resultType="ScoreMean";
					}
					if(operator==null&&"ScoreMean".equals(resultType)){
					    valType=null;
                    }
					String quanKey=cat+"_"+"appcount";
					if(valType==null){

						if("Num".equals(resultType)){
							results.put(param,Double.valueOf(apps2.size()));
							sb.append(" apps size");
						} else if(resultType.equals("ScoreMean")){
							double score=0;
							for(App app:apps){
								score+=app.getScore();
							}
							results.put(param,score/Double.valueOf(apps.size()));
							sb.append(" app mean score");

						}else if(resultType.equals("RTO")){
							sb.append(" app szie/ (allsize-syssize)");
							results.put(param,Double.valueOf(apps2.size())/Double.valueOf(distinctAppList.size()-sysAppCount));
						}
					}else if(valType!=null){
						Object compval=null;
						if(dataType==null&&dataTune!=null){
							compval=getDouble(dataTune.replace("K","000"));
                            sb.append(" const "+dataTune);
						}else if (dataType.equals("Quan")){
							quanKey=cat;
							quanKey+="_"+valType+"_"+dataTune.toLowerCase();
							if(catType.equals("CateLv2"))
								quanKey="smallcate_"+quanKey;
							compval=(String)dictInfoService.getDictInfo(quanKey,DictInfoCode.DICT_APP_QUAN_MODEL.getCode());
							if(compval!=null&&valType.equals("top")){
								compval=getList(compval+"");
							}
                            sb.append(" Quan "+quanKey);

                        }else if(dataType.equals("Mean")){
							String obj=(String)dictInfoService.getDictInfo(cat+"_"+valType,DictInfoCode.DICT_APP_MEAN_MODEL.getCode());

                            sb.append(" Mean "+cat+"_"+valType);
							if(dataTune!=null&&dataTune.length()>0){
								compval=getDouble(obj)*getDouble(dataTune);
							}else{
								compval=getDouble(obj);
							}
						}
						if(compval==null){
							results.put(param,0D);
							logger.error("比较较值为空{}",param);
						}else{
							sb.append(" compare val " + compval);
							int count = 0;
							double score = 0D;
							Map disMap=new HashMap();
							long start1=System.currentTimeMillis();
							if ("Le".equals(operator)) {

								double cv=getDouble(compval);

								for (App app : apps) {
									Double appVal = 0D;
									if (valType.equals("size"))
										appVal = app.getSize();
									else if (valType.equals("download"))
										appVal = app.getDownload();
									else if (valType.equals("score"))
										appVal = app.getScore();
									if (appVal > 0 &&((appVal <= cv&&!valType.equals("score"))||(appVal < cv&&valType.equals("score")))) {//0代表没有值
										//if(!disMap.containsKey(app.getName())){//去重
										//disMap.put(app.getName(),1);
										sb.append(" 命中app " + app.getName());
										count++;
										if (resultType.equals("ScoreMean")) {
											score += app.getScore();
										}
									}
								}
							}else if ("Ge".equals(operator)) {

								double cv=getDouble(compval);
								for (App app : apps) {
									Double appVal = 0D;
									if (valType.equals("size"))
										appVal = app.getSize();
									else if (valType.equals("download"))
										appVal = app.getDownload();
									else if (valType.equals("score"))
										appVal = app.getScore();
									if ( appVal >= cv) {
										//if(!disMap.containsKey(app.getName())){//去重
										//disMap.put(app.getName(),1);
										sb.append(" 命中app " + app.getName());
										count++;
										if (resultType.equals("ScoreMean")) {
											score += app.getScore();
										}
										//}

									}
								}
							}else if ("In".equals(operator)) {
								List<String> list = (List) compval;

								for (App app : apps) {

									if (list.contains(app.getName())) {
										//if(!disMap.containsKey(app.getName())) {
										//disMap.put(app.getName(), 1);
										sb.append(param + " 命中app " + app.getName());
										count++;
										//}
									}
								}
							}

							logger.info(" 遍历耗时："+(System.currentTimeMillis()-start1));
							sb.append(" operator " + operator).append(" " + resultType);
							if (resultType.equals("Num")) {
								results.put(param, Double.valueOf(count));
							} else if (resultType.equals("RTO")) {
								results.put(param, Double.valueOf(count) / Double.valueOf(apps.size()));
							} else if (resultType.equals("ScoreMean")) {//
								results.put(param, score / Double.valueOf(count));//应该是符合条件的平均分吧
							}
						}
					}
                    sb.append(" result "+results.get(param) +" 耗时："+(System.currentTimeMillis()-start));
                    logger.info(sb.toString());
					}

			}
			//logger.info(" -----------------param same count {}----------------------",samecount);
			logger.info("modelParam"+JSON.toJSONString(results));
			results.put("order_id",0D);
			double app_pred=loadPmmlRun.predict(results);
			int app_score=getScore(app_pred);

			logger.info("app_pred "+app_pred+" app_score "+app_score);
			res.setRiskScore(app_score);
			res.setResCode("0000");
			res.setResMsg("统计成功");
			return res;
			}catch (Exception ex){
				logger.error("计算随机森林模型出错{}",req,ex);//该抛异常还是返回错误代码？
			throw  new WsInvokeException("计算随机森林模型出错："+ex.getMessage());
		}
	}
	private static int getScore(double app_pred){
		int app_score=0;
		Double[] param_list=new Double[]{0.324013042D,0.526040562D,0.560038726D};

		if (app_pred < param_list[0]) {
			app_score = ((Double) (Math.ceil(app_pred * (300 / param_list[0])))).intValue();
		}
		else if (app_pred >=param_list[0] & app_pred < param_list[1]) {
			app_score = ((Double)(Math.ceil((app_pred -param_list[0]) *(350 / (param_list[1] -
					param_list[0]))+300))).intValue();

		}else if (app_pred >= param_list[1] & (app_pred < param_list[2])) {
			app_score = ((Double)(Math.ceil((app_pred -param_list[1]) *(150 / (param_list[2] -
					param_list[1])+650)))).intValue();
		} else {
			app_score = ((Double) (Math.ceil((app_pred - param_list[2]) * (200 / (1 - param_list[2])) + 800))).intValue();
		}
		return  app_score;
	}
	private double getDouble(Object val){
		if(val==null)
			return 0D;
		try {

			return  Double.valueOf(val.toString());
		}catch (Exception ex){
			logger.error("转换double出错{}",val,ex);
			ex.printStackTrace();
		}
		return  0D;
	}
	private Map readRandomCsv1() {
		String filePath = "/Users/baiyx/Desktop/app_model_191030.csv";
		Map<String, Float> data = new HashMap<String, Float>();
		CsvReader csvReader = null;
		try {
			char aa = 'a';
			FileInputStream inputStream = new FileInputStream(filePath);
			csvReader = new CsvReader(inputStream, Charset.defaultCharset());
			// 读表头
			csvReader.readHeaders();

			String[] headers = csvReader.getHeaders();
			// 读内容
			int count = 0;
			HashMap map = new HashMap();
			while (csvReader.readRecord()) {
//                for(int i=0;i<headers.length;i++){
////                    if(csvReader.get(i)==null||csvReader.get(i).toString().length()<=0)
////                        data.put(headers[i],0F);
////                    else
////                        data.put(headers[i],Float.valueOf(csvReader.get(i)));
//
//                }

				if("TRUE".equals(csvReader.get(12))){
					continue;
				}
				String id = csvReader.get(4).replace("_","");
				String name = csvReader.get(2);
				map.put(id, name);
				count++;
				//System.out.println("插入" + count + "条平均值计算");
			}

			inputStream.close();
			return map;
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	private  List getList(String val){
		if(val==null)return null;
		try {
			return  JSONArray.parseArray(val.replaceAll("'","\"")).toJavaList(String.class);
		}catch (Exception ex){
			logger.error("转换List 出错{}",val,ex);
		}
		return  null;
	}
	class App{
		public double getDownload() {
			return download;
		}

		public void setDownload(double download) {
			this.download = download;
		}

		public double getSize() {
			return size;
		}

		public void setSize(double size) {
			this.size = size;
		}

		public double getScore() {
			return score;
		}

		public void setScore(double score) {
			this.score = score;
		}

		private double download;
		private double size;
		private double score;

		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}

		private String name;

		public String getId() {
			return id;
		}

		public void setId(String id) {
			this.id = id;
		}

		private String id;
	}
}
